package com.zero.clinic.shiro;

import com.zero.clinic.pojo.User;
import com.zero.clinic.pojo.UserExample;
import com.zero.clinic.service.PermissionService;
import com.zero.clinic.service.UserService;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.util.ByteSource;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

public class MyRealm extends AuthorizingRealm {
    @Resource
    private UserService userService;

    @Resource
    private PermissionService permissionService;

    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
        System.out.println("认证开始了");
        // 1.从token令牌中获取身份信息，即登录的username
        String username = (String) token.getPrincipal();
        // 2.从数据中查询用户信息
        UserExample example = new UserExample();
        UserExample.Criteria criteria = example.createCriteria();
        criteria.andUsernameEqualTo(username);
        List<User> users = userService.selectByExample(example);
        // 3.如果users为空，说明用户不存在，则直接返回null
        if (users.size() == 0){
            return null;
        }
        // 4.如果users不为空，说明用户存在。则将从数据库中查询到的用户信息封装到authenticationInfo对象后返回
        User user = users.get(0);
        Object principal = user;
        Object hashedCredentials = user.getPassword();
        ByteSource credentialSalt = ByteSource.Util.bytes(user.getSalt());
        String realmName = this.getName();
        AuthenticationInfo authenticationInfo =
                new SimpleAuthenticationInfo(principal, hashedCredentials, credentialSalt, realmName);
        return authenticationInfo;
    }

    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
        System.out.println("授权开始了");
        //1.获取到当前认证成功的身份
        User user = (User)principals.getPrimaryPrincipal();
        //2.获取当前用户的权限信息,即所有的权限id字符串
        String jobPermissions = user.getJobPermissions();
        //3.根据权限id字符串得到权限id数组，然后根据权限id数组来查询所有权限id组成的权限表达式集合
        String[] jobPermissionsArr = jobPermissions.split(",");
        List<String> permissions = permissionService.selectExpressionsByPermissionIds(jobPermissionsArr);
        System.out.println("当前用户拥有的权限：" + permissions);
        //3.创建授权信息对象，将当前用户的权限信息添加到授权信息对象中并返回
        SimpleAuthorizationInfo authorizationInfo = new SimpleAuthorizationInfo();
        authorizationInfo.addStringPermissions(permissions);
        //4.将当前用户的角色（job）信息也添加到授权信息对象中，便于前端页面使用
        authorizationInfo.addRole(user.getJobName());  /*<shiro:hasRole name="挂号员"><shiro:hasRole>这样的情景会用*/
        return authorizationInfo;
    }
}
